{
 "cells": [
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "# 第五十一题 数组里面没有出现过的数字\n",
    "    给定一个长度为N的正整数数组nums,其中nums[i]的值都在区间[1,n]中，请你找出nums数组在[1,n]范围里面没有出现过的数字，并将它们放在数组里面返回（在数组里面的顺序可以不唯一）"
   ],
   "id": "b3723db8eb319c75"
  },
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-03-06T04:44:18.665155Z",
     "start_time": "2025-03-06T04:44:18.616967Z"
    }
   },
   "source": [
    "from fontTools.misc.cython import returns\n",
    "\n",
    "\n",
    "def func(nums):\n",
    "    l=[i for i in range(1,len(nums)+1)]\n",
    "    l2=[]\n",
    "    nums=set(nums)   #集合没有重复性，减少遍历次数\n",
    "    for o in l:\n",
    "        if o in nums:\n",
    "            continue\n",
    "        else:\n",
    "            l2.append(o)\n",
    "    return l2\n",
    "list=[1,2,3,4,2,1]\n",
    "print(func(list))"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[5, 6]\n"
     ]
    }
   ],
   "execution_count": 29
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-06T04:54:00.078511Z",
     "start_time": "2025-03-06T04:54:00.051812Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#给定一个长度为N的正整数数组nums,其中nums[i]的值都在区间[1,n]中，请你找出nums数组在[1,n]范围里面没有出现过的数字，并将它们放在数组里面返回（在数组里面的顺序可以不唯一）\n",
    "def func(nums):\n",
    "    l=(i for i in range(1,len(nums)+1) )\n",
    "    l2=[]\n",
    "    set=l  #集合，减少遍历次数\n",
    "    for m in l:\n",
    "        if m in nums:\n",
    "            continue\n",
    "        else:\n",
    "            l2.append(m)\n",
    "    return l2\n",
    "list=[1,2,3,4,2,1]\n",
    "print(func(list))"
   ],
   "id": "5fed9aceda30d8f",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[5, 6]\n"
     ]
    }
   ],
   "execution_count": 30
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "# 第五十二题 替换空格\n",
    "    将字符串中的空格替换为‘%’"
   ],
   "id": "8555be2c538e77af"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-05T23:41:50.532483Z",
     "start_time": "2025-03-05T23:41:50.506670Z"
    }
   },
   "cell_type": "code",
   "source": [
    "str1='Liu Yu xiao'\n",
    "str2=str1.replace(' ','%')   #字符串.replace(字符串1,字符串2)  字符串1替换为字符串2，得到新的字符串\n",
    "print(str2)"
   ],
   "id": "de9d9385514a7a49",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Liu%Yu%xiao\n"
     ]
    }
   ],
   "execution_count": 4
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-05T23:56:33.845128Z",
     "start_time": "2025-03-05T23:56:33.832929Z"
    }
   },
   "cell_type": "code",
   "source": [
    "def func(s):\n",
    "    res=' '\n",
    "    for i in s:\n",
    "        if i !=' ':\n",
    "            res +=i\n",
    "        else:\n",
    "            res +='%'\n",
    "    return res\n",
    "print(func('Liu Yu xiao'))\n",
    "            "
   ],
   "id": "4eda48f591c54e7c",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " Liu%Yu%xiao\n"
     ]
    }
   ],
   "execution_count": 7
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-06T05:00:29.771705Z",
     "start_time": "2025-03-06T05:00:29.756616Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 将字符串中的空格替换为‘%’\n",
    "def func(m):\n",
    "    str=' '\n",
    "    for i in m:\n",
    "        if i==' ':\n",
    "            str+='%'\n",
    "        else:\n",
    "            str+=i\n",
    "    return str\n",
    "n='Liu Yu xiao'\n",
    "print(func(n))\n",
    "            \n",
    "    "
   ],
   "id": "85e3113052ff557b",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " Liu%Yu%xiao\n"
     ]
    }
   ],
   "execution_count": 35
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "\n",
    "# 第五十三题 快乐数\n",
    "    给定一个正整数，请你判断这个数是不是快乐数\n",
    "    \n",
    "    快乐数：对于一个正整数，每次把他替换为他每个位置上的数字的平方和，如果这个数能变为1则是快乐数，如果不可能变为1，则不是快乐数   例如：正整数19  转换过程为1*1+9*9=82，8*8+2*2=68，6*6+8*8=100，1*1+0*0+0*0=1，所以他是快乐数"
   ],
   "id": "a54dfd4e8462b74"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-06T00:18:29.046441Z",
     "start_time": "2025-03-06T00:18:26.776948Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# n=19\n",
    "# func(n)#未处理循环情况，会导致无限递归\n",
    "# def func(num):\n",
    "#     ge=num%10\n",
    "#     shi=(num%100)//10\n",
    "#     bai=num//100\n",
    "#     result=ge*ge+shi*shi+bai*bai\n",
    "#     if result==1:\n",
    "#         return ('是快乐数')\n",
    "#     elif result==0:\n",
    "#         return ('不是快乐数')\n",
    "#     else:\n",
    "#         return func(result)\n",
    "#         "
   ],
   "id": "66adbdff96a5e0a0",
   "outputs": [
    {
     "ename": "RecursionError",
     "evalue": "maximum recursion depth exceeded",
     "output_type": "error",
     "traceback": [
      "\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[1;31mRecursionError\u001B[0m                            Traceback (most recent call last)",
      "Cell \u001B[1;32mIn[12], line 2\u001B[0m\n\u001B[0;32m      1\u001B[0m n\u001B[38;5;241m=\u001B[39m\u001B[38;5;241m19\u001B[39m\n\u001B[1;32m----> 2\u001B[0m func(n)\u001B[38;5;66;03m#未处理循环情况，会导致无限递归\u001B[39;00m\n\u001B[0;32m      3\u001B[0m \u001B[38;5;28;01mdef\u001B[39;00m \u001B[38;5;21mfunc\u001B[39m(num):\n\u001B[0;32m      4\u001B[0m     ge\u001B[38;5;241m=\u001B[39mnum\u001B[38;5;241m%\u001B[39m\u001B[38;5;241m10\u001B[39m\n",
      "Cell \u001B[1;32mIn[8], line 14\u001B[0m, in \u001B[0;36mfunc\u001B[1;34m(num)\u001B[0m\n\u001B[0;32m     12\u001B[0m     \u001B[38;5;28mprint\u001B[39m(\u001B[38;5;124m'\u001B[39m\u001B[38;5;124m不是快乐数\u001B[39m\u001B[38;5;124m'\u001B[39m)\n\u001B[0;32m     13\u001B[0m \u001B[38;5;28;01melse\u001B[39;00m:\n\u001B[1;32m---> 14\u001B[0m     func(result)\n",
      "Cell \u001B[1;32mIn[8], line 14\u001B[0m, in \u001B[0;36mfunc\u001B[1;34m(num)\u001B[0m\n\u001B[0;32m     12\u001B[0m     \u001B[38;5;28mprint\u001B[39m(\u001B[38;5;124m'\u001B[39m\u001B[38;5;124m不是快乐数\u001B[39m\u001B[38;5;124m'\u001B[39m)\n\u001B[0;32m     13\u001B[0m \u001B[38;5;28;01melse\u001B[39;00m:\n\u001B[1;32m---> 14\u001B[0m     func(result)\n",
      "    \u001B[1;31m[... skipping similar frames: func at line 14 (2974 times)]\u001B[0m\n",
      "Cell \u001B[1;32mIn[8], line 14\u001B[0m, in \u001B[0;36mfunc\u001B[1;34m(num)\u001B[0m\n\u001B[0;32m     12\u001B[0m     \u001B[38;5;28mprint\u001B[39m(\u001B[38;5;124m'\u001B[39m\u001B[38;5;124m不是快乐数\u001B[39m\u001B[38;5;124m'\u001B[39m)\n\u001B[0;32m     13\u001B[0m \u001B[38;5;28;01melse\u001B[39;00m:\n\u001B[1;32m---> 14\u001B[0m     func(result)\n",
      "\u001B[1;31mRecursionError\u001B[0m: maximum recursion depth exceeded"
     ]
    }
   ],
   "execution_count": 12
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-06T00:19:20.827305Z",
     "start_time": "2025-03-06T00:19:20.784378Z"
    }
   },
   "cell_type": "code",
   "source": [
    "def func(num, seen=None):\n",
    "    if seen is None:\n",
    "        seen = set()  # 初始化一个集合，用于记录已经计算过的数字\n",
    "\n",
    "    ge = num % 10\n",
    "    shi = (num % 100) // 10\n",
    "    bai = num // 100\n",
    "    result = ge * ge + shi * shi + bai * bai\n",
    "\n",
    "    if result == 1:\n",
    "        print('是快乐数')\n",
    "        return True\n",
    "    elif result in seen:  # 如果结果已经出现过，说明进入了循环!!!\n",
    "        print('不是快乐数')\n",
    "        return False\n",
    "    else:\n",
    "        seen.add(result)  # 将当前结果加入集合\n",
    "        return func(result, seen)  # 递归调用\n",
    "\n",
    "# 测试\n",
    "n = 19\n",
    "func(n)"
   ],
   "id": "b02f3a4cefa66d4c",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "是快乐数\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 13
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-06T00:23:50.679756Z",
     "start_time": "2025-03-06T00:23:50.655692Z"
    }
   },
   "cell_type": "code",
   "source": [
    "def change(x):\n",
    "    sum=0\n",
    "    while x>0:\n",
    "        j=x%10\n",
    "        sum+=j*j\n",
    "        x//=10\n",
    "    return sum\n",
    "\n",
    "def happynumber(n):\n",
    "    while n>9:\n",
    "        n=change(n)\n",
    "    if n==1:\n",
    "        return True\n",
    "    else:\n",
    "        return False\n",
    "    \n",
    "print(happynumber(19))"
   ],
   "id": "4ea2c2dfbe40a87c",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n"
     ]
    }
   ],
   "execution_count": 16
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": "#快乐数：对于一个正整数，每次把他替换为他每个位置上的数字的平方和，如果这个数能变为1则是快乐数，如果不可能变为1，则不是快乐数   例如：正整数19  转换过程为1*1+9*9=82，8*8+2*2=68，6*6+8*8=100，1*1+0*0+0*0=1，所以他是快乐数",
   "id": "827dfc235ebfd97e"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "# 第五十四题\n",
    "    给定一个正整数n，请你计算出这个数开立方根的结果。  结果向下取整"
   ],
   "id": "25a2e057b715c35c"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "# import math\n",
    "# pow(底数，指数)#指数幂"
   ],
   "id": "392659b7284ab48c"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-06T00:41:36.100517Z",
     "start_time": "2025-03-06T00:41:36.090624Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import math\n",
    "def func(n):\n",
    "    result=math.pow(n,1/3)\n",
    "    return int(result)\n",
    "\n",
    "print(func(28))"
   ],
   "id": "823e0b4c7ae4a4f0",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n"
     ]
    }
   ],
   "execution_count": 17
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "# 第五十五题 最长公共前缀\n",
    "    给你一个大小为n的字符串数组strs，其中包含n个字符串，编写一个函数来查找字符串数组中的最长公共前缀，返回这个公共前缀。   \n",
    "    输入：[\"abca\",\"abc\",\"abca\",\"abc\",\"abcc\"]\n",
    "    输出：\"abc\""
   ],
   "id": "62739b3f9640c687"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-06T00:47:52.535292Z",
     "start_time": "2025-03-06T00:47:52.510294Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 还没懂\n",
    "def func(strs):\n",
    "    n=len(strs)\n",
    "    if n==0:\n",
    "        return ''\n",
    "    for i in range(len(strs[0])):\n",
    "        temp=strs[0][i]\n",
    "        for j in range(1,n):\n",
    "            if i==len(strs[j]) or temp!=strs[j][i]:\n",
    "                return strs[0][0:i]\n",
    "    return strs[0]\n",
    "\n",
    "list=['abcd','abwk','abcdef']\n",
    "print(func(list))"
   ],
   "id": "44584916ff89a74f",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ab\n"
     ]
    }
   ],
   "execution_count": 19
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "",
   "id": "d6f297315d1cbb2a"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "# 第五十六题 字符串变形\n",
    "    首先这个字符串包含着一些空格，就像\"Hello World\"一样，然后我们要做的是把这个字符串中由空格隔开的单词反序，同时反转每个字符的大小写\n",
    "    比如：\"Hello World\"变形后就变成了\"wORLD hELLO\" "
   ],
   "id": "f52588006b511602"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-06T00:57:48.661336Z",
     "start_time": "2025-03-06T00:57:48.643096Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#swapcase()大小写转换\n",
    "def func(s):\n",
    "    l=s.split(' ')\n",
    "    l=l[::-1]\n",
    "    s=''\n",
    "    for i in l:\n",
    "        i=i.swapcase()\n",
    "        s+=i\n",
    "        s+=' '\n",
    "    return s[0:len(s)-1]\n",
    "print(func('Liu Yuxiao'))"
   ],
   "id": "2451a66b3dc3a8b6",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "yUXIAO lIU\n"
     ]
    }
   ],
   "execution_count": 20
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-06T08:30:58.123291Z",
     "start_time": "2025-03-06T08:30:58.100491Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#首先这个字符串包含着一些空格，就像\"Hello World\"一样，然后我们要做的是把这个字符串中由空格隔开的单词反序，同时反转每个字符的大小写   比如：\"Hello World\"变形后就变成了\"wORLD hELLO\"\n",
    "def func(s):\n",
    "    m=s.split(' ')  \n",
    "    m=m[::-1]#反序\n",
    "    s=''\n",
    "    for i in m:\n",
    "        i=i.swapcase()\n",
    "        s+=i\n",
    "        s+=' '\n",
    "    return s[0:len(s)-1]\n",
    "print(func('Liu Yuxiao'))\n",
    "    "
   ],
   "id": "5013310c26e67bba",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "yUXIAO lIU\n"
     ]
    }
   ],
   "execution_count": 39
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "# 第五十七题 压缩字符串\n",
    "    利用字符串重复出现的次数，编写一种方法，实现基本的字符串功能。比如，字符串aabnnnnnaaa会变为a2bc5a3\n",
    "    1.如果只有一个字符，1不用写\n",
    "    2.字符串中只包含大小写英文字母(a至z)"
   ],
   "id": "8c061cd38961a219"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-06T01:04:41.588577Z",
     "start_time": "2025-03-06T01:04:41.574266Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#kankan\n",
    "def func(s):\n",
    "    list=[]\n",
    "    for i in s:\n",
    "        if not list or list[-2]!=i:\n",
    "            list.append(i)\n",
    "            list.append(1)\n",
    "        else:\n",
    "            list[-1]+=1\n",
    "    return ''.join(map(str,[x for x in list if x!=1]))\n",
    "print(func('aaabbcc'))"
   ],
   "id": "134a666c9be38db5",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a3b2c2\n"
     ]
    }
   ],
   "execution_count": 21
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "def func(s):\n",
    "    result = []  # 用于存储压缩后的字符和计数\n",
    "    if not s:  # 如果输入字符串为空，直接返回空字符串\n",
    "        return \"\"\n",
    "\n",
    "    # 初始化第一个字符及其计数\n",
    "    current_char = s[0]\n",
    "    count = 1\n",
    "\n",
    "    # 遍历字符串，从第二个字符开始\n",
    "    for char in s[1:]:\n",
    "        if char == current_char:\n",
    "            count += 1  # 如果当前字符与前一个字符相同，计数加1\n",
    "        else:\n",
    "            result.append(current_char)  # 将前一个字符添加到结果中\n",
    "            result.append(str(count))  # 将计数添加到结果中\n",
    "            current_char = char  # 更新当前字符\n",
    "            count = 1  # 重置计数\n",
    "\n",
    "    # 添加最后一个字符及其计数\n",
    "    result.append(current_char)\n",
    "    result.append(str(count))\n",
    "\n",
    "    return ''.join(result)\n",
    "\n",
    "# 测试\n",
    "print(func('aaabbcc'))  # 输出：a3b2c2"
   ],
   "id": "b96a702e4d8f3984"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "# 第五十八题 三个数的最大乘积\n",
    "    给定一个长度为n的无序数组，包含正数、负数和0，请从中找出3个数，使得乘积最大，返回这个乘积"
   ],
   "id": "1f30629d7ab7b214"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-06T01:08:08.281627Z",
     "start_time": "2025-03-06T01:08:08.271746Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#kankan\n",
    "def func(list):\n",
    "    list.sort()\n",
    "    return max(list[0]*list[1]*list[-1],list[-1]*list[-2]*list[-3])\n",
    "print(func([0,34,-1,45]))"
   ],
   "id": "ffb60ec9a8a3479",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n"
     ]
    }
   ],
   "execution_count": 22
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 第五十九题 十进制转二进制",
   "id": "cc6c38dfba63568c"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-06T01:08:42.902639Z",
     "start_time": "2025-03-06T01:08:42.875558Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#十进制转二进制bin()\n",
    "a=10\n",
    "b=bin(a) #字符串\n",
    "print(b)"
   ],
   "id": "1004cab495c376a2",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0b1010\n"
     ]
    }
   ],
   "execution_count": 23
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "# 第六十题 判断字符是否唯一\n",
    "    给定一个字符串，请你判断其中每个字符是否全都不同"
   ],
   "id": "8ae2b81a4332dec9"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-06T01:14:20.406581Z",
     "start_time": "2025-03-06T01:14:20.398198Z"
    }
   },
   "cell_type": "code",
   "source": [
    "def func(str):\n",
    "    for i in range(len(str)):\n",
    "        if str[i] in str[i+1:]:\n",
    "            return False\n",
    "    return True\n",
    "print(func('LiuYuxiao'))"
   ],
   "id": "dc63e6647eb801ef",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n"
     ]
    }
   ],
   "execution_count": 28
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "",
   "id": "b76946195619f6d7"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-06T08:39:58.945287Z",
     "start_time": "2025-03-06T08:39:58.935984Z"
    }
   },
   "cell_type": "code",
   "source": [
    "def func(str):\n",
    "    for i in range(len(str)):\n",
    "        if str[i] in str[i-1]:\n",
    "            return False\n",
    "    return True\n",
    "print(func('LiuCao'))"
   ],
   "id": "86ab98ef41ddcf17",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n"
     ]
    }
   ],
   "execution_count": 40
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "",
   "id": "996f2d5781a8297"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
